package com.creditease.cn.monitor.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.creditease.cn.monitor.dao.redis.SlaveRedisNode;
import com.creditease.cn.monitor.service.MonitorService;

@Service
public class MonitorServiceImpl implements MonitorService{
	private Logger logger = LoggerFactory.getLogger(MonitorServiceImpl.class);
	@Autowired
	private SlaveRedisNode slaveRedisNode;

	public Map<String, String> getValue(String key) {
		Map<String,String> result = new HashMap<String, String>();
		Jedis jedis = slaveRedisNode.getConnection();
		result = jedis.hgetAll(key);
		return result;
	}

	public String getUsedMemory() {
		String value = "";
		Jedis jedis = slaveRedisNode.getConnection();
		String serverInfo = jedis.info();
		String[] serverInfos = serverInfo.split("\r\n");
		for(int i = 0;i<serverInfos.length;i++){
			if(serverInfos[i].contains(":")){
				String key = serverInfos[i].substring(0,serverInfos[i].indexOf(":"));
				if(key.equals("used_memory")){
					value = serverInfos[i].substring(serverInfos[i].indexOf(":")+1);
				}
			}
			
		}
		slaveRedisNode.close(jedis);
		logger.info("获取已使用内存 ：" + value);
		return value;
	}

}
